﻿using System;
using System.Reflection;


namespace VsTortoiseSVN
{
    public class ResourceManager
    {
        System.Resources.ResourceManager _resourceManager;

        #region Singleton
        private static ResourceManager _instance;
        public static ResourceManager Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new ResourceManager();

                return _instance;
            }
        }
        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        protected ResourceManager()
        {
            _resourceManager = CreateSatelliteResourceManager();
        }

        /// <summary>
        /// Creates a resource manager whose resources are located in a satellite dll.
        /// http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/51496efd-6125-48f6-b206-b60409d4e90c/
        /// </summary>
        private System.Resources.ResourceManager CreateSatelliteResourceManager()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();

            System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.GetCultureInfo("en-US");

            Assembly satelliteAssembly = assembly.GetSatelliteAssembly(culture);

            string removeThisText = ".resources";
            string manifestResourceName = satelliteAssembly.GetManifestResourceNames()[0];
            if (manifestResourceName.EndsWith(removeThisText, StringComparison.CurrentCultureIgnoreCase))
            {
                int startIndex = manifestResourceName.LastIndexOf(removeThisText, StringComparison.CurrentCultureIgnoreCase);
                manifestResourceName = manifestResourceName.Remove(startIndex, removeThisText.Length);
            }

            System.Resources.ResourceManager resourceManager = new System.Resources.ResourceManager(manifestResourceName, satelliteAssembly);
            return resourceManager;
        }


        /// <summary>
        /// Gets whether a string with the specified id exists
        /// in the satellite resource dll string table.
        /// </summary>
        public bool HasText(string id)
        {
            if (_resourceManager == null)
                return false;

            string text = _resourceManager.GetString(id);
            return !string.IsNullOrEmpty(text);
        }

        /// <summary>
        /// Gets the text of the string represented by id.
        /// If the text for the id is not available, the method will
        /// return the id instead.
        /// </summary>
        public string GetText(string id)
        {
            string text = "<" + id + ">"; ;
            if (_resourceManager == null)
                return text;

            text = _resourceManager.GetString(id);
            if (string.IsNullOrEmpty(text))
                return "<" + id + ">";

            return text;
        }
    }
}
